<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<script src="js/jquery.min.js" type="text/javascript" charset="utf-8"></script>

		<script type="text/javascript">
			$(function() {

				//全局记录token
				var token = "";

				$("#j_gettoken").on("click", function() {

					var userid = $("#j_userid").val();
					$.ajax({
						url: "http://localhost:50266/api/JwtTest/GetToken?userId=" + userid,
						type: "get",
						dataType: "json",
						cache: false,
						data: {},
						success: function(data) {
							console.log(data.status)
							console.log(data.msg)
							$("#j_token").text(data.msg);
							token = data.msg;
						},
						error: function(xhr) {
							console.log("出错了")
						}
					});
				})

				$("#j_gettokenmsg").on("click", function() {
					$.ajax({
						url: "http://localhost:50266/api/JwtTest/GetTokenMsg?token=" + token,
						type: "get",
						dataType: "json",
						cache: false,
						//headers: { "Authorization":"Bearer "+ token },
						beforeSend: function(xhr) {
							xhr.setRequestHeader('Authorization', 'Bearer ' + token);
							xhr.setRequestHeader('issuer', 'JwtBearer');
						},
						data: {},
						success: function(data) {
							$("#j_tokenmsg").text(data.msg);
						},
						error: function(xhr) {
							console.log(xhr)
							if (xhr.status == 401) {
								alert("授权失效，请重新获取")
							}else{
								alert("其他错误")
							}

						}
					});
				})
			})
		</script>

	</head>
	<body>
		<input id="j_userid" type="text" placeholder="输入userid" />
		<button id="j_gettoken" type="button"> 获取token</button>
		<button id="j_gettokenmsg" type="button"> 获取token信息</button>

		<div>
			<h2>token结果：</h2>
			<span id="j_token"></span>
		</div>
		<div>
			<h2>解密结果</h2>
			<span id="j_tokenmsg"></span>
		</div>
	</body>
</html>
